#########################################################################################################
#### Persistent Polarization: The Unexpected Durability of Political Animosity Around US Elections   ####
#### By: Neil Fasching, Shanto Iyengar, Yphtach Lelkes, and Sean J. Westwood                         ####
#### Last Edited: July 28, 2024                                                                      ####
#########################################################################################################

## Note: Scripts must be run in the order they appear in this file. ##

#### Libraries ####
if(!require('pacman')) {
  install.packages('pacman')
}
pacman::p_load(tidyverse, estimatr, haven, viridis, scales, sandwich, lmtest, ggpubr)

#### Working Directory ####
# Create an R Project such that this file (master.R) is in the top-level directory

# Suppress warnings (comment out to see warnings)
options(warn=-1)

#### Main Results ####

## Figure 1
# Note: This code may take some time to run.
# download data at https://doi.org/10.7910/DVN/XNHHDO
# file name: 'jopdataFINAL.dta'
# place in 'Reanalysis' folder
source("Code/Main_Results/Figure_1.R", print.eval = T)

## Figure 2:

# Load remaining packages
pacman::p_load(mirt, lme4, sjPlot, lm.beta, dplyr, rdrobust, TOSTER, 
               easystats, plyr, RColorBrewer, cowplot, texreg) 

#### Load Data ####
load('Data/final.RData')
statecode <- readRDS('Data/statecode.Rdata')
norms <- readRDS('Data/norms.rds')

#### Data Cleaning ####
source("Code/Main_Results/Data_Cleaning.R")

source("Code/Main_Results/Figure_2.R", print.eval = T)

## Figure 3:
source("Code/Main_Results/Figure_3.R", print.eval = T)

## Figure 4:
source("Code/Main_Results/Figure_4.R", print.eval = T)

#### Supplementary Materials ####

## Figure S1:
# Note: This code may take some time to run.
source("Code/Supplementary_Materials/Figure_S1.R", print.eval = T)

## Table S1:
source("Code/Supplementary_Materials/Table_S1.R", print.eval = T)

## Table S2:
source("Code/Supplementary_Materials/Table_S2.R", print.eval = T)

## Figure S2:
source("Code/Supplementary_Materials/Figure_S2.R", print.eval = T)

## Table S3:
source("Code/Supplementary_Materials/Table_S3.R", print.eval = T)

## Table S4:
source("Code/Supplementary_Materials/Table_S4.R", print.eval = T)

## Table S5:
source("Code/Supplementary_Materials/Table_S5.R", print.eval = T)

## Table S6:
source("Code/Supplementary_Materials/Table_S6.R", print.eval = T)

## Table S7:
source("Code/Supplementary_Materials/Table_S7.R", print.eval = T)

## Figure S3:
source("Code/Supplementary_Materials/Figure_S3.R", print.eval = T)

## Table S8:
source("Code/Supplementary_Materials/Table_S8.R", print.eval = T)

## Table S9:
source("Code/Supplementary_Materials/Table_S9.R", print.eval = T)

## Figure S4:
source("Code/Supplementary_Materials/Figure_S4.R", print.eval = T)

## Figure S5:
source("Code/Supplementary_Materials/Figure_S5.R", print.eval = T)

## Figure S6:
source("Code/Supplementary_Materials/Figure_S6.R", print.eval = T)

## Figure S7:
source("Code/Supplementary_Materials/Figure_S7.R", print.eval = T)

## Table S10:
source("Code/Supplementary_Materials/Table_S10.R", print.eval = T)

## Table S11:
source("Code/Supplementary_Materials/Table_S11.R", print.eval = T)

## Table S12:
source("Code/Supplementary_Materials/Table_S12.R", print.eval = T)

## Figure S8:
source("Code/Supplementary_Materials/Figure_S8.R", print.eval = T)

## Table S13:
# Need to download data from https://www.voterstudygroup.org/data/nationscape.
# There should be 63 Stata data files, from 'ns20191031.dta' to ns20210112.dta'.
# Place all files in 'Nationscape' folder, which is in the Data folder. 
source("Code/Supplementary_Materials/Table_S13.R", print.eval = T)

## Figure S9:
source("Code/Supplementary_Materials/Figure_S9.R", print.eval = T)

## Table S14:
source("Code/Supplementary_Materials/Table_S14.R", print.eval = T)

## Table S15:
source("Code/Supplementary_Materials/Table_S15.R", print.eval = T)

## Figure S10:
source("Code/Supplementary_Materials/Figure_S10.R", print.eval = T)

## END ##